package cn.echo.ecc.test;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradePagePayResponse response = Payment.Page().pay(
                    "springcloud入门", "2021000120616715",
                    "200", "http://localhost:6002/pay.success.html"
            );
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response.getBody());
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.getBody());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "2021000120616715";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCq/ZS5p0xxc7H5p2BJ+uyLjxw33AhTTlte/upT2ESuwbI1SlLnrqQ0JqEGgTObNxHArf2ccXOmEHcf1aBwVtYZE2pfVoU+F1BsEb5CRBpwzsxOviHRx4IqapNF/ciP6XJDhIX22PVtM+iYYfPUaWVd4qdNcsA6EzyGjkho5eLyUK6L6hAovcFD+fqZnqXZKJu41McKObI+Rpa3jjaW/CbpfgOMiMMCAjuC5i5q3Yw4mHmXdBIocQwh4McB4vspBTX7Y/f4MQotXR65NLz+B45WWltHeXd1dNFcsVjKvXPz9FrH860FCR9U7Sw8zoEmjDGlPuRBvV1CZWbO/6NcM9QVAgMBAAECggEAD4zQIbIdOZQT82hNUW0IZgi1wD66YxkVJiZ4mPdq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0bFlXfUcWGH6BSM+pL05HH9Kvc5K72OYO8LOifS5sFOvT6SQgjXxAR4kdy+KGca87H3aMnO5y96F/nRkxx/TmoXTJ3u5IyZ1dWIhY=";
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq8LyBe5GTE5WqIHqGMuZv5Pa3JK+Sv0eGmb4g9T2J7zINelhD/4+epTkHAixM9aE5H6x+CtUg64k4q+F2c+9N41XxRYRFXNMe9ANr66Rt1guGq6B23YROOOWBS64bXORu2XYOQ9M5MNsRQn8ENLCmau1M/yKkqoxQHT1ZmL9gWwrINnWDW23QlnQ8kw0GP450+H+pOj0aP+9+PmMDxYVQQeZwke8U9AMOh5lM9mx3RHnVLovyLsqFCvPoIYeiFbFLUYMWkX9YXMps4U8zSOIh7cOWyGpPCgw1BQyJhNRHiHYP0paAk6hLBBQzx+IndubKWjZL0d7Ci0ZkiYAusFz1QIDAQAB";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "";
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        config.encryptKey = "";
        return config;
    }
}